// 是否为闰年
function isLeapYear(year: number) {
  if ((year % 4 == 0) && ((year % 100) != 0) || (year % 400 == 0)) {
    return true;
  }
  else {
    return false;
  }

};
// 判断月份对应的日期
// 0:30天，
// 1:31天，
// 2:28天，
// 3:29天
function monthToDay(year: number, month: number) {
  let days = [30, 31, 29, 28]
  switch (month) {
    case 4:
    case 6:
    case 9:
    case 11:
      return days[0];
      break;
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
      return days[1];
      break
    case 2:
      if (isLeapYear(year)) {
        return days[2]
      } else {
        return days[3]
      };
      break;
  }
};
// 获取年份对应的月份和日期
function getMonth(year: number, startDate: any, endDate: Date) {
  let months = []
  let sm = startDate.getMonth() + 1
  let em = endDate.getMonth() + 1
  if (startDate.getFullYear() == year) {
    for (let i: any = sm; i <= 12; i++) {
      months.push(i)
    }
  } else if (endDate.getFullYear() == year) {
    for (let i = 1; i <= em; i++) {
      months.push(i)
    }
  } else {
    for (let i = 1; i <= 12; i++) {
      months.push(i)
    }
  }
  return months
};
function getDay(year: number, month: number, startDate: Date, endDate: Date) {
  let days = []
  let sd = startDate.getDate()
  let ed = endDate.getDate()
  let d = monthToDay(year, month)
  if (startDate.getFullYear() == year && startDate.getMonth() + 1 >= month) {
    for (let i = sd; i <= d; i++) {
      days.push(i)
    }
  } else if (endDate.getFullYear() == year && endDate.getMonth() + 1 <= month) {
    for (let i = 1; i <= ed; i++) {
      days.push(i)
    }
  } else {
    for (let i = 1; i <= d; i++) {
      days.push(i)
    }
  }
  return days
}
function getYear(startDate: Date, endDate: Date) {
  let years = []
  console.log(startDate.getFullYear())
  console.log(endDate.getFullYear())
  for (let i = startDate.getFullYear(); i <= endDate.getFullYear(); i++) {
    years.push(i)
  }
  return years
}

const getDateInfo = (year: number, month: number, startDate: string, endDate: string) => {
  console.log('11111')
  let start = new Date(startDate)
  let end = new Date(endDate)
  let months = getMonth(year, start, end)
  let days = getDay(year, month, start, end)
  console.log('111')
  let years = getYear(start, end)
  return {
    years,
    months,
    days
  }
}

export default getDateInfo
